Let’s do some polling! Go to pollev.com/kumarr436
What do the students already know? You may be able to answer this from program structure, or through a survey.
Sometimes students will be coming in with different levels of background knowledge. You can try to address this before the session and/or adjust your teaching accordingly.
Where can students get background knowledge before the session?
learnr tutorialsRMarkdown (and RProjects)Motivation
Help students feel comfortable
I usually outline the core content by:
I strongly suggest an examples and exercises approach to teaching skills in R. We’ll practice this in a moment.
I always like to end with:
Open the RProject file and look in the working directory: you will see an exercises subdirectory and an answers subdirectory.
The following lesson snippets all use .R code files for the exercises. You can also ask students to use .Rmd, especially if this is part of a course where you will need to collect assignment submissions.
ggplot() functiongeom functions, such as geom_point() or geom_hist()aes() function nested within ggplot() or a geom function+ operator# Load data
gapminder <- gapminder::gapminder
# Look at the structure of the data. You can use glimpse(), summary(), or head().
glimpse(gapminder)## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
These will produce the same output:
ggplot(gapminder) +
geom_point(aes(x=year, y=pop)) +
labs(title="Population over time", x="Year", y="Population")Plot life expectancy as a function of GDP per capita for the year 2007, and add labels.
gapminder07 to ggplot()geom_point() + Supply x=gdpPercap and y=lifeExp to aes()title, x, and y in labs()ggplot(gapminder07) +
geom_point(aes(x=gdpPercap, y=lifeExp)) +
labs(title="Do people in richer countries live longer?", x="GDP per capita", y="Life expectancy")There are may geom functions we can choose to generate geometric objects:
Let’s try to add geom_smooth() to the previous plot we created.
ggplot(gapminder07, aes(x=gdpPercap, y=lifeExp)) +
geom_point() +
geom_smooth() +
labs(title="Do people in richer countries live longer?", x="GDP per capita", y="Life expectancy", subtitle="Gapminder 2007 data")## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(gapminder07) +
geom_point(aes(x=gdpPercap, y=lifeExp)) +
geom_hline(aes(yintercept=mean(lifeExp))) +
labs(title="Do people in richer countries live longer?", x="GDP per capita", y="Life expectancy", subtitle="Gapminder 2007 data")Plot the life expectancy of each continent in 2007.
Look at the ggplot cheatsheet and decide which kind of geom to use.
ggplot(gapminder07) +
geom_boxplot(aes(x=continent, y=lifeExp)) +
labs(title="Distribution of life expectancy", x="Continent", y="Distribution")ggplot(gapminder07) +
geom_col(aes(x=continent, y=lifeExp)) +
labs(title="Distribution of life expectancy", x="Continent", y="Distribution")You can think of the continent or year variables as grouping variables: they place each observation in one of several groups.
We can represent the groups through aesthetic mapping or facets rather than along one of the axes.
ggplot(gapminder) +
geom_point(aes(x=gdpPercap, y=lifeExp, col=year)) +
labs(title="Do people in richer countries live longer?", x="GDP per capita", y="Life expectancy")ggplot(gapminder, aes(x=gdpPercap, y=lifeExp)) +
geom_point() +
geom_smooth() +
facet_wrap(~year) +
labs(title="Do people in richer countries live longer?", x="GDP per capita", y="Life expectancy")## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Try adding the argument scales="free" to the facet_wrap() layer.
ggplot(gapminder, aes(x=gdpPercap, y=lifeExp)) +
geom_point() +
geom_smooth() +
facet_wrap(~year, scales="free") +
labs(title="Do people in richer countries live longer?", x="GDP per capita", y="Life expectancy")## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Visualize life expectancy by continent in 2007 again. This time, group continents by color or facet.
ggplot(gapminder07, aes(x=gdpPercap, y=lifeExp,
col=continent, size=pop)) +
geom_point() +
labs(title="Life expectancy as a function of GDP per capita, by continent", x="GDP per capita", y="Life expectancy")ggplot(gapminder07, aes(x=lifeExp)) +
geom_density() +
facet_wrap(~continent, scales="free") +
labs(title="Life expectancy as a function of GDP per capita, by continent", x="GDP per capita", y="Life expectancy")Choose your own adventure!
Create a plot that includes two geoms and facets
RMarkdown, RProjects, and RStudio itself.Examples from NU:
Other resources:
learnr interactive tutorials, from RStudioTake it to the next level (suggestions from Christina Maimone):